package com.dao.impl;
// default package

import static org.hibernate.criterion.Example.create;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.dao.UserDao;
import com.model.User;
import com.util.Pagination;

/**
 	* A data access object (DAO) providing persistence and search support for User entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .User
  * @author MyEclipse Persistence Tools 
 */
@Repository("UserDao")
public class UserDaoImpl implements UserDao {
	
	@Resource
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}



    
	public boolean save(User user) {
		Session session = sessionFactory.getCurrentSession();
		try{
			session.save(user);	
			return true;
		} catch(RuntimeException re){
			throw re;
		}
		
	}
    
	public void deleteById(int uid) {
        
        	Session session = sessionFactory.getCurrentSession();
    		User u = (User) session.get(User.class, uid); 
    		session.delete(u);
    }
    
    public User findById( java.lang.Integer id) {
        try {
        	Session session = sessionFactory.getCurrentSession();
            User instance = (User) session.get("com.model.User", id);
            return instance;
        } catch (RuntimeException re) {
            throw re;
        }
    }
    
    
    public List<User> findByExample(User instance) {
    	Session session = sessionFactory.getCurrentSession();
    	List<User> results = (List<User>) session
                   .createCriteria("com.model.User")
                   .add( create(instance) )
                   .list();
           return results;
       
    }    
    
	

    public List<User> findAll() {
    	String queryString = "from User";
    	Session session = sessionFactory.getCurrentSession();
    	Query queryObject = session.createQuery(queryString);
    	
    	return  (List<User>)queryObject.list();
    }
    
    
	
    public User merge(User detachedInstance) {
    	   try {
    		   Session session = sessionFactory.getCurrentSession();
    	       User result = (User) session
    	               .merge(detachedInstance);
    		   
    	       return result;
    	   } catch (RuntimeException re) {
    	       throw re;
    	   }
    	}
    
    public List<User> getUserList(User user, Pagination page) {
    	Session session = sessionFactory.getCurrentSession();
    	List<User> results = (List<User>) session
                   .createCriteria("com.model.User")
                   .add( create(user) ).setFirstResult((page.getCurrentpage()-1)*page.getPagesize()-1).setMaxResults(page.getPagesize())
           .list();
    	
           return results;
       
    	
    }

	@Override
	public User editUser(User user) {
		int id = user.getId();
		Session session = sessionFactory.getCurrentSession();
		//User u = (User) session.get("com.model.User",id);
		Query q = session.createQuery("from User u where u.id = "+id);
		User u = (User) q.list().get(0);
		u.setCollege(user.getCollege());
		u.setEmail(user.getEmail());
		u.setMajor(user.getMajor());
		u.setQq(user.getQq());
		u.setStunum(user.getStunum());
		u.setUserpassword(user.getUserpassword());
		u.setTel(user.getTel());
		u.setRealname(user.getRealname());
		session.update(u);
		return u;
		
	}

	@Override
	public Pagination stuList(Pagination paperPage,String flag) {
		String hql ;
		if(flag.equals("stu"))
			hql="from User u where u.role=0 ";
		else
			hql="from User u where u.role=1";
		
		Session session = sessionFactory.getCurrentSession();
		Query q = session.createQuery(hql);
		paperPage.setTotal(q.list().size());
		paperPage.setTotalpagecountByhand();
		q.setFirstResult((paperPage.getCurrentpage()-1)*paperPage.getPagesize());
		q.setMaxResults(10);
		paperPage.setList(q.list());
		return paperPage;
	}
}